package com.foreveross.action.weixin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.HrFavorableService;
import com.foreveross.service.LogService;
import com.foreveross.service.OrderInfoService;
import com.foreveross.util.JSONUtil;
import com.foreveross.vo.HrCode;
import com.foreveross.vo.HrFavorable;
import com.foreveross.vo.OrderInfo;
import com.foreveross.vo.common.FileVo;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;
import com.foreveross.vo.condition.FavorableCondition;
/**
 * 润优惠
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/weixin/HrFavorableController")
public class HrFavorableController {

	private Logger log = Logger.getLogger(HrFavorableController.class);

	@Resource
	private HrFavorableService hrFavorableService;
	@Resource
	private OrderInfoService orderInfoService;

	
	@Resource
	private LogService logService;
	
	/**
	 * 获取润优惠 LIST
	 * @param request
	 * @param response
	 * @param currentPage
	 * @param pageSize
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getFavorableList")
	public void getFavorableList(HttpServletRequest request,
			HttpServletResponse response,
			String currentPage,String pageSize) {
		ResultVo resultVo = new ResultVo();
		try {
			int total = (int)hrFavorableService.getFavorableListCount(CommonUtil.getWeixinID(request), null, null, null, null);
			PageVo<HrFavorable> pageVo = new PageVo<HrFavorable>(currentPage,total,pageSize);
			List<HrFavorable> list = hrFavorableService.getFavorableList(CommonUtil.getWeixinID(request),null, null, null, null,pageVo.getpageIndex(),pageVo.getpageSize());
			
			//图片地址转换
			List<HrFavorable> listOut = new ArrayList<HrFavorable>(); 
			for (HrFavorable hrFavorable : list){
				List<FileVo> fileVoList = FileVo.getFileVoList(request, hrFavorable.getImage());
				hrFavorable.setOther(fileVoList);
				listOut.add(hrFavorable);
			}
			pageVo.setData(listOut);
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
			log.info("润优惠 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优惠  查看失败");
			log.error("润优惠 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 查看润优惠详情
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getFavorableById")
	public void getFavorableById(HttpServletRequest request,
			HttpServletResponse response,String id) {
		ResultVo resultVo = new ResultVo();
		try {
			HrFavorable favorable = hrFavorableService.getFavorableById(id);
			List<FileVo> fileVoList = FileVo.getFileVoList(request, favorable.getImage());
			resultVo.setOther(fileVoList);
			resultVo.setSuccess(true);
			resultVo.setData(favorable);
			log.info("润优惠 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优惠  查看失败");
			log.error("润优惠 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 查看润优惠详情
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getFavorableListByIds")
	public void getFavorableListByIds(HttpServletRequest request,
			HttpServletResponse response,String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			List<HrFavorable> HrFavorableList = new ArrayList<HrFavorable>();
			PageVo<HrFavorable> pageVo = new PageVo<HrFavorable>();
			if(ids != null && !"".equals(ids)){
				pageVo = new PageVo<HrFavorable>("1",ids.split(",").length,String.valueOf(Constants.PAGE_MAX_SIZE));
				for(String id:ids.split(",")){
					HrFavorable favorable = hrFavorableService.getFavorableById(id);
					if(favorable!=null){
						List<FileVo> fileVoList = FileVo.getFileVoList(request, favorable.getImage());
						favorable.setOther(fileVoList);
						HrFavorableList.add(favorable);
					}
				}
			}
			pageVo.setData(HrFavorableList);
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
			log.info("润优惠 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优惠  查看失败");
			log.error("润优惠 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/saveOrderInfo")
	public void saveOrderInfo(HttpServletRequest request,
			HttpServletResponse response,String favorableConditionJson) {
		ResultVo resultVo = new ResultVo();
		try {
			String weixinId =CommonUtil.getWeixinID(request);
			FavorableCondition favorableCondition = JSONUtil.jsonToClazz(favorableConditionJson, FavorableCondition.class);
			Map<String,Object> map = orderInfoService.saveOrderInfo(weixinId, favorableCondition);
			resultVo.setData(map);
			resultVo.setSuccess(true);
			log.info("润优惠 订单提交成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优惠 订单 " + Constants.UPDATE_ADD + "失败");
			log.error("润优惠 订单" + Constants.UPDATE_ADD + "失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
//	@RequestMapping(method = RequestMethod.POST, value = "/updateOrderInfo")
//	public void updateOrderInfo(HttpServletRequest request,
//			HttpServletResponse response, String orderIds,boolean payStatus,
//			String payType,String outTradeNo) {
//		ResultVo resultVo = new ResultVo();
//		try {
//			orderInfoService.updateOrderInfo(orderIds, payStatus, payType, outTradeNo);
//
//			resultVo.setSuccess(true);
//			log.info("润优惠 订单修改 成功!");
//		} catch (Exception e) {
//			resultVo.setSuccess(false);
//			resultVo.setMessage("润优惠 订单 " + Constants.UPDATE_ADD + "失败");
//			log.error("润优惠 订单" + Constants.UPDATE_ADD + "失败!", e);
//		}
//		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
//	}

}
